//
//  CameraViewController.m
//  Topit
//
//  Created by dllo on 15/12/14.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "CameraViewController.h"
#import "RootViewController.h"
@interface CameraViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation CameraViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if([[[UIDevice
          currentDevice] systemVersion] floatValue]>=8.0) {
        
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
        
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.7;
    [self.view addSubview:view];
    
    UIView *cameraView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.width, 30)];
    UIImageView *photographView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 30, 30)];
    photographView.image = [UIImage imageNamed:@"photograph"];
    [cameraView addSubview:photographView];
    [photographView release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(photographView.right + 10, 5, 90, 20)];
    label.text = @"拍照";
    label.textColor = [UIColor whiteColor];
    [cameraView addSubview:label];
    [label release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, self.view.width, 30);
    [button addTarget:self action:@selector(photographDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [cameraView addSubview:button];
    
    [view addSubview:cameraView];
    [cameraView release];
    
    UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, self.view.width, 30)];
    UIImageView *photoImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 30, 30)];
    photoImage.image = [UIImage imageNamed:@"photo"];
    [photoView addSubview:photoImage];
    [photoImage release];
    
    UILabel *photoLabel = [[UILabel alloc] initWithFrame:CGRectMake(photoImage.right + 10, 0, 90, 30)];
    photoLabel.text = @"本地相册";
    photoLabel.textColor = [UIColor whiteColor];
    [photoView addSubview:photoLabel];
    [photoLabel release];
    
    UIButton *photoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    photoButton.frame = CGRectMake(0, 0, self.view.width, 30);
    [photoButton addTarget:self action:@selector(photoDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [photoView addSubview:photoButton];
    
    [view addSubview:photoView];
    [photoView release];
    
    UIView *sweepView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, self.view.width, 30)];
    UILabel *sweep = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 30)];
    sweep.text = @"扫一扫";
    sweep.textColor = [UIColor whiteColor];
    sweep.textAlignment = 1;
    [sweepView addSubview:sweep];
    [sweep release];
    
    UIButton *sweepButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sweepButton.frame = CGRectMake(0, 0, self.view.width, 30);
    [sweepButton addTarget:self action:@selector(sweepDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [sweepView addSubview:sweepButton];
    [view addSubview:sweepView];}

- (void)photographDidPress:(UIButton *)sender {
    [self addPicEvent];
}
- (void) addPicEvent {
    //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未检测到相机" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;    
    picker.sourceType = sourceType;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    UIImage *image = [[info objectForKey:UIImagePickerControllerEditedImage] retain];
    [self performSelector:@selector(saveImage:)
               withObject:image
               afterDelay:0.5];
}
- (void)saveImage:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
    
}



- (void)photoDidPress:(UIButton *)sender {
    [self addPhoto];
}

- (void) addPhoto {
    //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

- (void)sweepDidPress:(UIButton *)sender {
    RootViewController *rootVC = [[RootViewController alloc] init];
    [self presentViewController:rootVC animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
